<?php
header('Content-Type: application/json');
Header('Access-Control-Allow-Methods:GET,POST,PUT,DELETE');
header("Access-Control-Allow-Headers: Content-Type");
header('Access-Control-Allow-Origin:*');   //跨域

require './mysql_init.php';



//获取商品
function get_goods($pdo,$goods_id){
    try {
        // 准备查询语句
        $query = "SELECT goods_id, name, price,count, cover_img_path FROM goods where count >0 and is_undercarriag=0 and goods_id > ? ORDER BY goods_id ASC LIMIT 3";
        $statement = $pdo->prepare($query);
        $statement->bindParam(1, $goods_id);
        // 执行查询
        $statement->execute();
        // 获取结果
        $result = $statement->fetchAll();      //把查询结构赋值给$count
        return $result;
    } catch (PDOException $e) {
        // 异常处理
        // echo "Error querying database: " . $e->getMessage();
        return -1; // 表示查询出错
    }
}

if($_SERVER['REQUEST_METHOD'] === 'GET'){
    $response = [
        'status' => 0,
        'message' => '初始化'
    ];
    $goods_id=$_GET['goods_id'];    //获取最后一个goods_id
    $data=get_goods($pdo,$goods_id);
    if($data !=-1 && count($data)>0){
        $response = [
            'status' => 1,
            'message' => '成功',
            'data' =>$data
        ];
    }else{
        
        $response = [
            'status' => 0,
            'message' => '失败',
            'data' =>'空'
        ];
    }
    echo json_encode($response);
}else{
    $response = [
        'status' => 0,
        'message' => '不是get请求'
    ];
    echo json_encode($response);
}



?>